<?php
namespace ac\modules\developer\controllers;

use Yii;
use ac\controllers\AuthController;
use app\modules\developer\models\AppCategory;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;
use common\behaviors\Sort;


class AppCategoryController extends AuthController
{
    public function behaviors()
    {
        return [
             // 注册匿名行为，只有行为类名
            Sort::className(),

        ];
    }

	/*APP分类列表*/
    public function actionCategoryList()
    {
        $where = '';
        if(isset($this->get['display']) and $this->get['display'] == 2){
            $where = 'display = 2';
        }
        $appCategory = new AppCategory;
        $appCategoryList = $appCategory::find()->where($where)
                           ->orderBy(['sort' => SORT_DESC])->asArray()->all();
        return Tool::showRes([0, '获取成功'], $appCategoryList);
    }

    /*添加APP分类*/
    public function actionAddCategory()
    {
        if(Yii::$app->request->isPost){
            $appCategory = new AppCategory;
            if($appCategory->addCategory($this->post)){
                $backInfo = [0, '添加APP分类成功'];//返回信息
            }else{
                if($appCategory->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $appCategory->getErrors();//异常信息
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*
    修改APP分类
    $flag               值为setDisplay时 作用是更改状态，为空是修改APP分类信息（更改APP分类状态的方法调用传参）
    $successBackInfo    成功的返回信息（更改APP分类状态的方法调用传参）
    */
    public function actionModifyCategory($flag = '', $successBackInfo = '')
    {
        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            $id = isset($this->get['id'])?$this->get['id']:'';
            if($id){
                $appCategory = AppCategory::find()->where('app_cat_id = :id', [':id' => $id])->asArray()->one();
                if(!is_null($appCategory)){
                    return Tool::showRes([0, '获取成功'], $appCategory);
                }else{
                    return Tool::showRes(ErrMap::getError(10010));
                };
            }
        }

        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['AppCategory']['id'])?intval($this->post['AppCategory']['id']):'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $appCategory = new AppCategory;
                if(empty($flag)){
                    $successBackInfo = '修改APP分类成功';//修改企业信息
                }
                if($appCategory->modifyCategory($this->post, $id, $flag)){
                    $backInfo = [0, $successBackInfo];//返回信息
                }else{
                    if($appCategory->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $appCategory->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*是否显示1-不显示-2显示（是否显示操作）*/
    public function actionSetDisplay()
    {
        if(Yii::$app->request->isPost){
            return $this->actionModifyCategory('setDisplay', '设置APP分类状态成功');
        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*排序*/
    public function actionSetSort()
    {
        if(Yii::$app->request->isPost){
            $sort = $this->post['sort'];
            // P($sort);
            if(!empty($sort) and is_array($sort)){
                if($this->setSortList(new AppCategory, $sort)){
                    $backInfo = [0, '设置应用分类排序成功'];//返回信息
                }

            }
            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), $exception) ) != 'boolean'){
                return $res;
            };
        }
        return Tool::showRes(ErrMap::getError(10005));
    }


    /*删除APP分类,（真删除）*/
    public function actionDeleteCategory()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $appCategory = new AppCategory;
            if($appCategory->deleteAll('app_cat_id = :id', [':id' => $id])){//删除成功
                $backInfo = [0, '删除APP分类成功'];//返回信息
            }else{//删除失败
                $backInfo = ErrMap::getError(10013, '删除APP分类失败');//返回信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog($this->operationLogArgs($backInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));
    }



}